#! /bin/sh
#set -x


#####################################
# READ PARAMS
#
app=$1
arch=$2


#####################################
echo 
echo "*** SCRIPT install_vcomponents_v4rev ***"
echo "app = $app"
echo "arch = $arch"
echo 


#####################################
# PREPARE variables:

# NOTE that "app"/Contents specify slash. 
# this is not a problem even if app itself have slash at end.
# We will get something as  gg.app//Contents and this still works fine.
#
if [ "$arch" = "64" ] ; then
	sys_vcomp=/usr/local/lib/vcomponents_x64
	dllsuff="_x64"
	v8lib="libv8_64.dylib"
else	
	sys_vcomp=/usr/local/lib/vcomponents
	dllsuff=""
	v8lib="libv8_32.dylib"
fi

app_vcomp="$app"/Contents/vcomponents$dllsuff
exec_vcomp="@executable_path/../vcomponents$dllsuff"


# YOU can change this to 0 to skip VREPORT installation
install_vreports=1


echo "#####################################"
echo "# COPY files of vcomponents into APP."
echo "#####################################"
echo 

if [ ! -d "$app_vcomp" ] ; then
	mkdir "$app_vcomp"
fi

# VSHARED
if [ ! -e "$app_vcomp"/libvshared_fat_release$dllsuff.dylib ] ; then
	cp $sys_vcomp/libvshared_fat_release$dllsuff.dylib 	"$app_vcomp"
fi

# VKERNEL
if [ ! -e "$app_vcomp"/libvkernel_fat_release$dllsuff.dylib ] ; then
	cp $sys_vcomp/libvkernel_fat_release$dllsuff.dylib 	"$app_vcomp"
fi

# VCLIENT
if [ ! -e "$app_vcomp"/libvclient_fat_release$dllsuff.dylib ] ; then
	cp $sys_vcomp/libvclient_fat_release$dllsuff.dylib 	"$app_vcomp"
fi

# VREPORT
if [ ! -e "$app_vcomp"/libvreport_fat_release$dllsuff.dylib ] ; then
	cp $sys_vcomp/libvreport_fat_release$dllsuff.dylib 	"$app_vcomp"
fi

# JavaScript V8 engine
if [ ! -e "$app_vcomp"/$v8lib ] ; then
	cp $sys_vcomp/$v8lib 	"$app_vcomp"
fi


echo "####################################"
echo "# Copy Folders:"
echo "####################################"
echo

# vresources
cp -R $sys_vcomp/vresources 					"$app_vcomp"


echo "#####################################"
echo "# CHANGE executable_path for engine dylibs:"
echo "#####################################"
echo

	# change id of VSHARED
	install_name_tool -id "$exec_vcomp/libvshared_fat_release$dllsuff.dylib" "$app_vcomp"/libvshared_fat_release$dllsuff.dylib

	# change id of VKERNEL
	install_name_tool -id "$exec_vcomp/libvkernel_fat_release$dllsuff.dylib" "$app_vcomp"/libvkernel_fat_release$dllsuff.dylib

	install_name_tool -change "$sys_vcomp/libvshared_fat_release$dllsuff.dylib"   "$exec_vcomp/libvshared_fat_release$dllsuff.dylib" 	"$app_vcomp"/libvkernel_fat_release$dllsuff.dylib
	install_name_tool -change "$sys_vcomp/VSDK.framework/Versions/A/VSDK" "$exec_vcomp/VSDK.framework/Versions/A/VSDK" 	"$app_vcomp"/libvkernel_fat_release$dllsuff.dylib

	# change id of VCLIENT
	install_name_tool -id "$exec_vcomp/libvclient_fat_release$dllsuff.dylib" "$app_vcomp"/libvclient_fat_release$dllsuff.dylib
	install_name_tool -change "$sys_vcomp/libvshared_fat_release$dllsuff.dylib" "$exec_vcomp/libvshared_fat_release$dllsuff.dylib" "$app_vcomp"/libvclient_fat_release$dllsuff.dylib

	# change id of VREPORT
	install_name_tool -id "$exec_vcomp/libvreport_fat_release$dllsuff.dylib" "$app_vcomp"/libvreport_fat_release$dllsuff.dylib
	install_name_tool -change "$sys_vcomp/libvshared_fat_release$dllsuff.dylib" "$exec_vcomp/libvshared_fat_release$dllsuff.dylib" 	"$app_vcomp"/libvreport_fat_release$dllsuff.dylib
	install_name_tool -change "$sys_vcomp/libvkernel_fat_release$dllsuff.dylib" "$exec_vcomp/libvkernel_fat_release$dllsuff.dylib" 	"$app_vcomp"/libvreport_fat_release$dllsuff.dylib
	install_name_tool -change "$sys_vcomp/libvclient_fat_release$dllsuff.dylib" "$exec_vcomp/libvclient_fat_release$dllsuff.dylib" 	"$app_vcomp"/libvreport_fat_release$dllsuff.dylib
	install_name_tool -change "$sys_vcomp/$v8lib"                               "$exec_vcomp/$v8lib"                   	            "$app_vcomp"/libvreport_fat_release$dllsuff.dylib

    
    
echo "#####################################"
echo "now we need to update yet APP/Contents/MacOS/Externals/v4rev.bundle/Contents/MacOS/v4rev"
echo "#####################################"
echo

app_v4rev="$app"/Contents/MacOS/Externals/v4rev.bundle/Contents/MacOS/v4rev

if [ -e "$app_v4rev" ] ; then
	install_name_tool -change "$sys_vcomp/libvshared_fat_release$dllsuff.dylib" 	"$exec_vcomp/libvshared_fat_release$dllsuff.dylib" 		"$app_v4rev"
	install_name_tool -change "$sys_vcomp/libvkernel_fat_release$dllsuff.dylib" 	"$exec_vcomp/libvkernel_fat_release$dllsuff.dylib" 		"$app_v4rev"
	install_name_tool -change "$sys_vcomp/libvclient_fat_release$dllsuff.dylib" 	"$exec_vcomp/libvclient_fat_release$dllsuff.dylib" 		"$app_v4rev"
	install_name_tool -change "$sys_vcomp/libvreport_fat_release$dllsuff.dylib" 	"$exec_vcomp/libvreport_fat_release$dllsuff.dylib" 		"$app_v4rev"
fi

echo "#####################################"
echo "now we need to update yet APP/Contents/MacOS/Externals/dbvalentina/Contents/MacOS/dbvalentina"
echo "#####################################"
echo

app_dbvalentina="$app"/Contents/MacOS/Externals/database_drivers/dbvalentina/Contents/MacOS/dbvalentina

if [ -e "$app_dbvalentina" ] ; then
	install_name_tool -change "$sys_vcomp/libvshared_fat_release$dllsuff.dylib" 	"$exec_vcomp/libvshared_fat_release$dllsuff.dylib" 		"$app_dbvalentina"
	install_name_tool -change "$sys_vcomp/libvkernel_fat_release$dllsuff.dylib" 	"$exec_vcomp/libvkernel_fat_release$dllsuff.dylib" 		"$app_dbvalentina"
	install_name_tool -change "$sys_vcomp/libvclient_fat_release$dllsuff.dylib" 	"$exec_vcomp/libvclient_fat_release$dllsuff.dylib" 		"$app_dbvalentina"
	install_name_tool -change "$sys_vcomp/libvreport_fat_release$dllsuff.dylib"     "$exec_vcomp/libvreport_fat_release$dllsuff.dylib" 		"$app_dbvalentina"
fi


echo
echo "Done."
echo
